package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-02-01 20:09
 **/
public class LeetCode1763 {

    public String longestNiceSubstring(String s) {
        int n = s.length();
        int begin = 0;
        int maxLen = 0;
        for (int i = 0; i < n; i++) {
            //小写字母和大写字母用二进制伟表示,从右往左每一位表示是否存在一个字母(A-Z，a-z)
            int lower = 0;
            int upper = 0;
            for (int j = i; j < n; ++j) {
                char c = s.charAt(j);
                if (Character.isLowerCase(c)) {
                    //置某位为1 a-z -> 1-26位
                    lower |= 1 << (c - 'a');
                } else {
                    upper |= 1 << (c - 'A');
                }

                //lower和upper相等说明每个字母都存在大小写的形式
                if (lower == upper && j - i + 1 > maxLen) {
                    begin = i;
                    maxLen = j - i + 1;
                }
            }
        }

        return s.substring(begin, begin + maxLen);
    }

}
